package com.stxx.stiot2.domain.tools;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.stxx.stiot2.domain.SysSocial;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;

public class Build {

    public static void main(String[] args) {
        build(SysSocial.class);
    }

    static void build(Class<?> clazz){
        String classNname = clazz.getSimpleName();
        createService(classNname);
        createServiceImpl(classNname);
        createController(classNname);
        createHttpJs(classNname);
        createPageDir(classNname);
        System.out.println("=============================ok==========================");
    }

    static void createService(String classNname){
        String tpl = "package com.stxx.stiot2.service;\n" +
                "import com.stxx.stiot2.domain."+classNname+";\n" +
                "import com.stxx.framwork.support.BaseService;\n" +
                "/**\n" +
                " * @author fuzhenlei\n" +
                " * @date "+ DateUtil.format(new Date(),"yyyy-MM-dd HH:mm:ss") +"\n" +
                " */\n" +
                "public interface "+classNname+"Service extends BaseService<"+classNname+",String> {\n" +
                "\n" +
                "}";
        Boolean r = write(classNname,System.getProperty("user.dir")+"\\src\\main\\java\\com\\stxx\\stiot2\\service\\"+classNname+"Service.java",tpl);
        if(r){
            System.out.println("创建"+classNname+"Service"+"成功");
        }else{
            System.out.println("创建"+classNname+"Service"+"失败，已经存在");
        }
    }
    static void createServiceImpl(String classNname){
        String tpl = "package com.stxx.stiot2.service.impl;\n" +
                "\n" +
                "import com.stxx.framwork.domain.model.PageQuery;\n" +
                "import com.stxx.framwork.domain.model.PageResult;\n" +
                "import com.stxx.framwork.utils.StoreUtils;\n" +
                "import com.stxx.stiot2.domain."+classNname+";\n" +
                "import com.stxx.stiot2.service."+classNname+"Service;\n" +
                "import org.noear.solon.annotation.Component;\n" +
                "import org.noear.solon.annotation.Inject;\n" +
                "import java.sql.SQLException;\n" +
                "import java.util.List;\n" +
                "\n" +
                "/**\n" +
                " * @author Fuzhenlei\n" +
                " * @date "+ DateUtil.format(new Date(),"yyyy-MM-dd HH:mm:ss") +"\n" +
                " */\n" +
                "@Component\n" +
                "public class "+classNname+"ServiceImpl implements "+classNname+"Service{\n" +
                "    @Inject\n" +
                "    StoreUtils storeUtils;\n" +
                "\n" +
                "    @Override\n" +
                "    public "+classNname+" queryById(String id) {\n" +
                "        try {\n" +
                "            return storeUtils.selectById(id,"+classNname+".class);\n" +
                "        } catch (SQLException e) {\n" +
                "            throw new RuntimeException(e);\n" +
                "        }\n" +
                "    }\n" +
                "\n" +
                "    @Override\n" +
                "    public int insert("+classNname+" bean) throws SQLException {\n" +
                "        validEntityBeforeSave(bean);\n" +
                "        return storeUtils.insert(bean);\n" +
                "    }\n" +
                "\n" +
                "    @Override\n" +
                "    public int update("+classNname+" bean) throws SQLException {\n" +
                "        validEntityBeforeSave(bean);\n" +
                "        return storeUtils.update(bean);\n" +
                "    }\n" +
                "\n" +
                "    /**\n" +
                "     * 保存前的数据校验\n" +
                "     */\n" +
                "    private void validEntityBeforeSave("+classNname+" bean){\n" +
                "\n" +
                "    }\n" +
                "\n" +
                "\n" +
                "\n" +
                "    @Override\n" +
                "    public int delete(String name) throws SQLException {\n" +
                "        return storeUtils.delete("+classNname+".class,name);\n" +
                "    }\n" +
                "\n" +
                "    @Override\n" +
                "    public int deleteByIds(String[] ids) throws SQLException {\n" +
                "        return storeUtils.deleteByIds("+classNname+".class, ids);\n" +
                "    }\n" +
                "\n" +
                "    @Override\n" +
                "    public List<"+classNname+"> list("+classNname+" obj) throws SQLException {\n" +
                "        return storeUtils.selectList("+classNname+".class,obj);\n" +
                "    }\n" +
                "\n" +
                "    @Override\n" +
                "    public PageResult<"+classNname+"> pageList("+classNname+" obj, PageQuery pageQuery) throws SQLException {\n" +
                "        return storeUtils.selectPageList("+classNname+".class,obj,pageQuery);\n" +
                "    }\n" +
                "}\n";

        Boolean r = write(classNname,System.getProperty("user.dir")+"\\src\\main\\java\\com\\stxx\\stiot2\\service\\impl\\"+classNname+"ServiceImpl.java",tpl);
        if(r){
            System.out.println("创建"+classNname+"ServiceImpl"+"成功");
        }else{
            System.out.println("创建"+classNname+"ServiceImpl"+"失败，已经存在");
        }
    }
    static void createController(String className){
        String name = StrUtil.toUnderlineCase(className).replace("_","-");
        String tpl = "package com.stxx.stiot2.controller;\n" +
                "\n" +
                "import com.stxx.framwork.domain.R;\n" +
                "import com.stxx.framwork.domain.model.PageQuery;\n" +
                "import com.stxx.framwork.domain.model.PageResult;\n" +
                "import com.stxx.stiot2.domain."+className+";\n" +
                "import com.stxx.stiot2.service."+className+"Service;\n" +
                "import org.noear.solon.annotation.*;\n" +
                "\n" +
                "import java.sql.SQLException;\n" +
                "\n" +
                "@Controller\n" +
                "@Mapping(\"/"+name+"\")\n" +
                "public class "+className+"Controller {\n" +
                "\n" +
                "    @Inject\n" +
                "    "+className+"Service baseService;\n" +
                "\n" +
                "    @Get\n" +
                "    @Mapping(\"/list\")\n" +
                "    public PageResult<"+className+"> list("+className+" bo, PageQuery pageQuery) throws SQLException {\n" +
                "        PageResult<"+className+"> r = baseService.pageList(bo,pageQuery);\n" +
                "        return r;\n" +
                "    }\n" +
                "    \n" +
                "    @Post\n" +
                "    @Mapping(\"\")\n" +
                "    public R<Object> add(@Body "+className+" bo) throws SQLException {\n" +
                "        baseService.insert(bo);\n" +
                "        return R.ok(\"数据保存成功\");\n" +
                "    }\n" +
                "\n" +
                "    @Put\n" +
                "    @Mapping(\"\")\n" +
                "    public R<Object> edit(@Body "+className+" bo) throws SQLException {\n" +
                "        baseService.update(bo);\n" +
                "        return R.ok(\"数据保存成功\");\n" +
                "    }\n" +
                "\n" +
                "    @Delete\n" +
                "    @Mapping(\"/{ids}\")\n" +
                "    public R<Object> remove(@Path String[] ids) throws SQLException {\n" +
                "        baseService.deleteByIds(ids);\n" +
                "        return R.ok(\"数据删除成功\");\n" +
                "    }\n" +
                "}\n";

        Boolean r = write(className,System.getProperty("user.dir")+"\\src\\main\\java\\com\\stxx\\stiot2\\controller\\"+className+"Controller.java",tpl);
        if(r){
            System.out.println("创建"+className+"Controller"+"成功");
        }else{
            System.out.println("创建"+className+"Controller"+"失败，已经存在");
        }
    }
    static void createHttpJs(String className){
        String name = StrUtil.toUnderlineCase(className).replace("_","-");
        String tpl = "module.exports={\n" +
                "    add(data){\n" +
                "        return request({\n" +
                "            url: '/"+name+"',\n" +
                "            headers: {},\n" +
                "            method: 'post',\n" +
                "            data: data\n" +
                "        });\n" +
                "    },\n" +
                "    edit(data){\n" +
                "        return request({\n" +
                "            url: '/"+name+"',\n" +
                "            headers: {},\n" +
                "            method: 'put',\n" +
                "            data: data\n" +
                "        });\n" +
                "    },\n" +
                "    remove(data){\n" +
                "        return request({\n" +
                "            url: '/"+name+"/'+data,\n" +
                "            headers: {},\n" +
                "            method: 'delete'\n" +
                "        });\n" +
                "    },\n" +
                "    list(data){\n" +
                "        return request({\n" +
                "            url: '/"+name+"/list',\n" +
                "            headers: {},\n" +
                "            method: 'get',\n" +
                "            params: data\n" +
                "        });\n" +
                "    }\n" +
                "}\n";

        Boolean r = write(className,System.getProperty("user.dir")+"\\src\\main\\resources\\static\\http\\"+name+".js",tpl);
        if(r){
            System.out.println("创建"+className+"Http.js"+"成功");
        }else{
            System.out.println("创建"+className+"Http.js"+"失败，已经存在");
        }
    }

    static void createPageDir(String className){
        String name = StrUtil.toUnderlineCase(className).replace("_","-");
        File path = new File(System.getProperty("user.dir")+"\\src\\main\\resources\\static\\view\\"+name);
        if(!path.exists()){
            path.mkdirs();
        }
    }


    static Boolean write(String classNname,String pathStr,String tpl){
        File path = new File(pathStr);
        if(path.exists()){
            return false;
        }
        try {
            FileWriter writer = new FileWriter(path);
            writer.write(tpl);
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return true;
    }



}
